	
	$(document).ready(function(){
	$('.video_play_icon').center();
	
	$('#relatedPosts').gridalicious({
		gutter: 5,
		width: 100,
		animate: true,
	  	animationOptions: {
		    queue: true,
		    speed: 100,
		    duration: 300,
		    effect: 'fadeInOnAppear',
	  	}	
	});
	/*
	 * Show quick view of a thread
	 */
	 $('.itemImage').live('click', function(){
		
		blankId = $(this).parent().parent().attr('data-href');
		postType = $(this).parent().parent().attr('data-rel');
		
		if(!blankId){
			return;
		}
		
		popupControl = getPopupControl(blankId, postType);
		modalBox(blankId, postType, popupControl);
		
	});


	$('.controlNavItem').live('click', function(e){
		
		e.preventDefault();
		$('#infscr-loading').remove();
		$('.popupControl').hide(); 
		$('.modal').modal('hide');
		navigation = $(this).attr('id');
		id = $(this).parent().parent().parent().attr('data-href');
		postType = $(this).parent().parent().parent().attr('data-rel');

		if(navigation == 'nextNav'){
			nextItemId = $('#innerItem-'+id+'-'+postType).parent().next().find('.innerItem').attr('data-href');
			nextItemType = $('#innerItem-'+id+'-'+postType).parent().next().find('.innerItem').attr('data-rel');
		}else{
			nextItemId = $('#innerItem-'+id+'-'+postType).parent().prev().find('.innerItem').attr('data-href');
			nextItemType = $('#innerItem-'+id+'-'+postType).parent().prev().find('.innerItem').attr('data-rel');
		}
		

		if(!nextItemId){
			alert('Hết dữ liệu trong trang. Load thêm để xem nhé!');
			return;
		}

		popupControl = getPopupControl(nextItemId, nextItemType);
		
		modalBox(nextItemId, nextItemType, popupControl);
	
	});

	
	//$("#wrapHeader").sticky({topSpacing:0});
	//$("#sectionControl").sticky({topSpacing:0});
	
	
	$.scrollUp({
	    scrollName: 'toTop', // Element ID
	    scrollDistance: 1, // Distance from top before showing element (px)
	    topSpeed: 100, // Speed back to top (ms)
	    animation: 'fade', // Fade, slide, none
	    animationInSpeed: 200, // Animation in speed (ms)
	    animationOutSpeed: 200, // Animation out speed (ms)
	    scrollText: '<img src="public/images/top.png" alt="To top" />', // Text for element
	    activeOverlay: false, // Set CSS color to display scrollUp active point, e.g '#00FFFF'
	  });

	


	


	$('.modal').live('hide.bs.modal', function () {
	    if(!$(this).attr('data-href')){
			    
			$('.popupControl').hide(); 
			var id = $(this).attr('id');
	
			iframe = $('#'+id).find('iframe.embed_video')
			src = iframe.attr('src');
			iframe.attr('src', '');
			iframe.attr('src', src);
	    }
	});
	
	/*
	 *  Report post
	 */
	 $('.report_post').live('click',function(e){
		e.preventDefault();
		dataHref = $(this).parent().parent().parent().attr('data-href');
		dataRel = $(this).parent().parent().parent().attr('data-rel');
		if(!$('#modal_report_' + dataHref).length){
			
			$.ajax({

				url: baseUrl + 'index.php?module=ajax&act=ajax&get_modal_report=1',
				type: 'POST',
				data: {post_id : dataHref, rel: dataRel,},
				success: function(data){
					$('body').append(data);
					$('#modal_report_' + dataHref).modal();
					//$('#modal_report').show();
				}
			});
			
		}else{
			$('#modal_report_' + dataHref).modal();
		}
	});

	$('.submit_report').live('click', function(e){
		e.preventDefault();
		modalContent = $(this).attr('data-href');
		modalRel = $(this).attr('data-rel');
		$(".alert").remove();
			$.ajax({
				url: baseUrl + 'index.php?module=ajax&act=ajax&send_report=1',
				type: 'POST',
				data: { 
					txt_report_content: $('#txt_report_content_' + $(this).attr('data-href')).val(),
					post_id: $(this).attr('data-href'),
					rel: $(this).attr('data-rel')
				},
				success: function(data){
					if(data) newData = JSON.parse(data);
					if(newData.errors){
	    				$.each(newData.errors, function(e, error){
		    				console.log(e);
		    				console.log(error);
		    				if(e != 'error'){
		    					$('#' + e).parent().append('<div id="'+e+'_error" class="alert alert-danger alert-dismissable" style="display: none;"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>'+error+'</div>');
		    					$('#' + e + '_error').slideDown('fast');
		    				}
	    				});
					}else if(newData.message){
						
						//console.log($('#txt_report_content_' + $(this).attr('data-href')).val());
						$('#modal-content-' + modalContent).append('<div id="report_success_'+modalContent+'" class="alert alert-success alert-dismissable" style="display:none;">Cám ơn bạn đã báo cáo nội dung xấu. Chúng tôi sẽ kiểm tra bài viết trong thời gian sớm nhất.</div>');
						//$('#report_success').slideDown('fast');
						var test = $('#report_success_' + modalContent).slideDown('fast').delay(1000).slideUp('fast', function(){$('#modal_report_' + modalContent).modal('hide');});
						$('#txt_report_content_' + modalContent).val('');
						
					}
				}
			});
		});
	});

		function modalBox(id, postType, popupControl){

			if(!$('#popupElem-innerItem-' + id + '-' + postType).text().length){
				$('#innerItem-'+id+'-'+postType).append('<div role="dialog" id="popupElem-innerItem-' + id + '-' + postType + '" class="modal" style="display: none;">');
				$('#popupElem-innerItem-' + id + '-' + postType).append('<div class="modal-dialog modal-wide" id="content-innerItem-'+ id +'">');
				$('#popupElem-innerItem-' + id + '-' + postType).append('</div><!-- end .modal-dialog --></div><!-- end .modal .fade -->');
				$('#popupElem-innerItem-'+id+'-'+postType+ ' > div.modal-dialog').append('<div id="loadMoreItems" class="normalLoadBtn" style="margin-top:20px;"><img  width="20" src="public/images/loading.gif" alt="loading" /> Loading </div><!-- end #loadMoreItems -->');
				$('#popupElem-innerItem-' + id + '-' + postType).modal();
				ajaxCallRequest(id, postType, popupControl);
			}else{
				$('#popupElem-innerItem-' + id + '-' + postType).modal();
				$('#popupControl-innerItem-'+id+'-'+postType).show();
				//FB.XFBML.parse(document.getElementById('popupControl-innerItem-'+ id+'-'+postType));
			}
		}

		function ajaxCallRequest(id, postType, popupControl){
		
			$.ajax({
				url : baseUrl + 'index.php?module=ajax&act=ajax&ajaxGetNews=1',
				type : 'POST',
				data : {id: id, postType: postType},
				
				success: function(data){
					
					$('#popupElem-innerItem-' + id + '-' + postType +' > div.modal-dialog > div#loadMoreItems').remove();
					newData = $.parseJSON(data);
					
					if(newData.data){
						
						$('#popupElem-innerItem-' + id + '-' + postType + ' > #content-innerItem-' + id).append(newData.data);
						$('#wrapAll').append(popupControl);
						$('#popupControl-innerItem-' + id).sticky();
						FB.XFBML.parse(document.getElementById('popupControl-innerItem-'+ id+'-'+postType));
						FB.XFBML.parse(document.getElementById('popupElem-innerItem-' + id + '-' + postType));
					}
				},
			});
		}

		function getPopupControl(id, postType){
			
			popupControl = '<div class="popupControl" id="popupControl-innerItem-'+id+'-'+postType+'" data-href="'+id+'" data-rel="'+postType+'">';
			popupControl += '<div class="wrap-fb-like-popup">	<span class="pull-left" style="padding:5px; font-size:15px; font-weight:bold; color:#399"><i class="icon-thumbs-up-alt"></i> Thích Và Share </span>  <div class="facebook-btn pull-left" style="width:200px; margin-top:5px; margin-left:5px"><fb:like href="http://dongcam.vn/t85256" send="true" layout="button" width="200" show_faces="false" font="" label="Post"></fb:like></div>';
			popupControl += '<div class="clr"></div></div>';
			
			popupControl += '<div class="controlNav">';
			
			popupControl += '<div class="btn-group pull-right">';
			popupControl += '<button type="button" class="report_post btn btn-danger"  ><i class="icon-warning-sign"></i>  Bài Viết Xấu</button>';
			popupControl += '<button type="button" id="preNav" class="controlNavItem btn btn-primary"  ><i class="icon-long-arrow-left"></i>Trước</button>';
			popupControl += '<button type="button" id="nextNav" class="controlNavItem btn btn-primary" > Sau <i class="icon-long-arrow-right"></i> </button>';
			popupControl += '</div>';
			popupControl += '</div>';
			
			popupControl += '</div><!-- end .popupControl -->';
			return popupControl;
		}
		
		function popupMessage(title, message, type){
			$modal = '<div class="modal fade" id="popup_message" style="display:none"> \
				  <div class="modal-dialog">\
			    <div class="modal-content">\
			      <div class="modal-header">\
			        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>\
			        <h4 class="modal-title">Modal title</h4>\
			      </div>\
			      <div class="modal-body">\
			        <p>One fine body&hellip;</p>\
			      </div>\
			      <div class="modal-footer">\
			        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>\
			        <button type="button" class="btn btn-primary">Save changes</button>\
			      </div>\
			    </div><!-- /.modal-content -->\
			  </div><!-- /.modal-dialog -->\
			</div><!-- /.modal -->';
			
			$(body).append($modal);
			$('#popup_message').modal('');
		}
		

		
	
